package tool.lib.volley;

import java.io.File;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageLoader;

public class VolleyTool {
	private static VolleyTool volleyInstance=null;
	private RequestQueue mRequestQueue;
   // private ImageLoader mImageLoader;
	private VolleyTool(Context context) {
		File cacheDir = new File(context.getCacheDir(), "image_cache");
		String userAgent = "tubu";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }
        Network network = new BasicNetwork(new HurlStack());
        DiskBasedCache diskBasedCache=new DiskBasedCache(cacheDir,1024*1024*100);
        mRequestQueue = new RequestQueue(diskBasedCache, network);
        mRequestQueue.start();
    	//mImageLoader = new ImageLoader(mRequestQueue,new SuperImageCache(context));
    }
    public static void init(Context context){
        if(volleyInstance == null){
        	volleyInstance = new VolleyTool(context);
        }
    }
    public static VolleyTool getInstance()
    {
    	return volleyInstance;
    }
    public static void release()
    {
    	volleyInstance=null;
    }
	public RequestQueue getRequestQueue() {
		return mRequestQueue;
	}

//	public ImageLoader getImageLoader() {
//		return mImageLoader;
//	}
    public void add(Request request)
    {
        mRequestQueue.add(request);
    }
}
